<?php
/**
 * 快速测试语音播放（使用修复后的方法）
 * 运行：php test_voice_quick.php
 */

error_reporting(E_ALL);
ini_set('display_errors', 'on');

echo "========================================\n";
echo "   快速语音测试\n";
echo "========================================\n\n";

// 测试 MP3 播放
echo "1. 测试播放 MP3 文件...\n";
$mp3File = __DIR__ . '/voice/checkin_success.mp3';

if (is_file($mp3File)) {
    echo "   文件: {$mp3File}\n";
    $realPath = realpath($mp3File);
    echo "   真实路径: {$realPath}\n";
    
    // 使用 mciSendString 方法（修复后的方法）
    $tempScript = sys_get_temp_dir() . '/play_voice_test.ps1';
    $psContent = str_replace('FILE_PATH', str_replace('\\', '\\\\', $realPath), <<<'POWERSHELL'
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class AudioPlayer {
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string command, System.Text.StringBuilder returnValue, int returnLength, IntPtr winHandle);
    
    public static void Play(string file) {
        mciSendString("close MediaFile", null, 0, IntPtr.Zero);
        mciSendString("open \"" + file + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
        mciSendString("play MediaFile", null, 0, IntPtr.Zero);
    }
}
"@
[AudioPlayer]::Play("FILE_PATH")
Write-Output "播放完成"
POWERSHELL
);
    
    file_put_contents($tempScript, $psContent);
    echo "   临时脚本: {$tempScript}\n";
    echo "   正在播放...\n";
    
    $command = 'powershell -WindowStyle Normal -ExecutionPolicy Bypass -File "' . $tempScript . '"';
    exec($command, $output, $returnVar);
    
    echo "   返回值: {$returnVar}\n";
    if (!empty($output)) {
        echo "   输出: " . implode("\n   ", $output) . "\n";
    }
    
    @unlink($tempScript);
    echo "   ✓ 完成\n\n";
} else {
    echo "   ✗ 文件不存在: {$mp3File}\n\n";
}

// 测试 WAV 播放
echo "2. 测试播放 WAV 文件...\n";
$wavFile = __DIR__ . '/voice/checkin_success.wav';

if (is_file($wavFile)) {
    echo "   文件: {$wavFile}\n";
    $realPath = realpath($wavFile);
    echo "   真实路径: {$realPath}\n";
    
    $psCommand = '$player = New-Object Media.SoundPlayer "' . str_replace('\\', '/', $realPath) . '"; $player.Play(); Write-Output "播放完成"';
    $command = 'powershell -WindowStyle Normal -Command "' . $psCommand . '"';
    
    echo "   正在播放...\n";
    exec($command, $output, $returnVar);
    
    echo "   返回值: {$returnVar}\n";
    if (!empty($output)) {
        echo "   输出: " . implode("\n   ", $output) . "\n";
    }
    echo "   ✓ 完成\n\n";
} else {
    echo "   ✗ 文件不存在: {$wavFile}\n\n";
}

// 测试 TTS
echo "3. 测试 Windows TTS...\n";
$text = '签到成功';
$psCommand = 'Add-Type -AssemblyName System.Speech; ' .
             '$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer; ' .
             '$speak.Rate = 0; ' .
             '$speak.Volume = 100; ' .
             '$speak.Speak(\'' . $text . '\'); ' .
             'Write-Output "播放完成"';

$command = 'powershell -WindowStyle Normal -Command "' . $psCommand . '"';

echo "   文本: {$text}\n";
echo "   正在播放...\n";
exec($command, $output, $returnVar);

echo "   返回值: {$returnVar}\n";
if (!empty($output)) {
    echo "   输出: " . implode("\n   ", $output) . "\n";
}
echo "   ✓ 完成\n\n";

echo "========================================\n";
echo "   测试完成！\n";
echo "   如果听到了声音，说明修复成功！\n";
echo "========================================\n";

